# -*- coding: utf-8 -*-
import re
import time
import json
import redis
import random
class HttpProxyMiddleware(object):
    def __init__(self, conf):
        self.conf = conf
        self.logger = conf.log
        self.redis = redis.Redis(host=conf.redis_proxies['host'], port=conf.redis_proxies['port'], password=conf.redis_proxies['password'], db='8',
                                   retry_on_timeout=True, socket_timeout = 300, socket_connect_timeout=300)
        self.proxy_key = self.redis.keys()
        end = len(self.proxy_key)
        i = 0
        while i < end:
            if not re.search('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', self.proxy_key[i]):
                self.proxy_key.pop(i)
                end -= 1
            else:
                i += 1
        self.localhost = ({'domains': {}})
        self.proxy_key.append('localhost')
        self.proxy_len = len(self.proxy_key)

    def process_request(self, request):
        domain = request.url.split('/')[2]
        self.proxy_len = len(self.proxy_key)
        end = random.randint(0, self.proxy_len-1)
        i = (end + 1) % self.proxy_len
        while i != end:
            try:
                if self.proxy_key[i] == 'localhost':
                    proxy = self.localhost
                else:
                    proxy = self.redis.get(self.proxy_key[i])
                    proxy = json.loads(proxy)
                if not proxy.get('domains'):
                    proxy['domains'] = {}
                now = time.time()
                if not proxy['domains'].get(domain) or now - proxy['domains'][domain] > request.info['proxy_time']:
                    proxy['domains'][domain] = now
                    if self.proxy_key[i] != 'localhost':
                        self.redis.set(self.proxy_key[i], json.dumps(proxy))
                        request.info['proxy'] = proxy
                        request.info['proxy']['proxy_key'] = self.proxy_key[i]
                    return
            except Exception as e:
                self.logger.error(e)
            i = (i + 1) % self.proxy_len
        time.sleep(request.info['proxy_time'])
